package com.csdn.swaggershirojwtredis.service.impl;

import com.csdn.swaggershirojwtredis.dao.LampstateDao;
import com.csdn.swaggershirojwtredis.entity.Lampstate;
import com.csdn.swaggershirojwtredis.service.LampstateService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 灯状态信息(Lampstate)表服务实现类
 *
 * @author 张川
 * @since 2019-05-17 16:56:42
 */
@CacheConfig(cacheNames = "lampstate",cacheManager = "cacheManager")
@Service("lampstateService")
public class LampstateServiceImpl implements LampstateService {
    @Resource
    private LampstateDao lampstateDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Cacheable(key="'queryById-'+#id",unless="#result == null")
    @Override
    public Lampstate queryById(Integer id) {
        return this.lampstateDao.queryById(id);
    }

  /**
     *通过实例对象查询数据
     *
     * @param: Lampstate
     * @return: 对象列表
     */
    @Override
    public List< Lampstate> queryAll(Lampstate lampstate) {
       return this.lampstateDao.queryAll(lampstate);
    }
    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Cacheable(key="#offset+'-'+#limit")
    @Override
    public List<Lampstate> queryAllByLimit(int offset, int limit) {
        return this.lampstateDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param lampstate 实例对象
     * @return 实例对象
     */
    @Override
    public Lampstate insert(Lampstate lampstate) {
        this.lampstateDao.insert(lampstate);
        return lampstate;
    }

    /**
     * 修改数据
     *
     * @param lampstate 实例对象
     * @return 实例对象
     */
    @Override
    public Lampstate update(Lampstate lampstate) {
        this.lampstateDao.update(lampstate);
        return this.queryById(lampstate.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.lampstateDao.deleteById(id) > 0;
    }
}
